<template>
	<view class="fillOrderWrapper" v-if="show">
		<!-- <view class="title">{{title}}</view>
		<view class="infoBox">
			<view class="info">设备名称：{{eqName}}</view>
			<view class="info">检点时间：{{nowTime}}</view>
		</view> -->
		<view class="infoBox">
			<view class="title">基本信息</view>
			<view class="remark">
				<text>备注：</text>
				<u-input class="el-input" v-model="remark" type="textarea" :border="true" placeholder='请输入备注'></u-input>
			</view>
			<view class="itemWRapper">
				<view class="comItemBox" v-for="(e,i) in htmlRes" :key="i">
					<view class="comItem" :style="{width:item.span*100/24 + '%'}" v-if='isShowItem(item)'  v-for="(item,index) in e" :key="index">
						<text class="title_label" v-if='item.showLabel'><text v-if='item.required' style="color: red;">*</text>{{item.label}}</text>
						<text v-if='item.tag=="row-title"'>{{item.titleText}}</text>
						<u-radio-group v-if='item.tag=="el-radio-group"' v-model="item.defaultValue" @change="radioGroupChange(item.vModel)">
							<u-radio 
								v-for="(opt, opti) in item.slot.options" :key="opti"
								:name="opt.value"
							>
								{{opt.label}}
							</u-radio>
						</u-radio-group>
						<u-input class="el-input" v-if="item.tag=='el-input'" v-model="item.defaultValue" :type="item.tagIcon" :border="true" :placeholder='item.placeholder'></u-input>
						<view class="file" v-if="item.tag=='el-upload'" >
							<u-upload ref="uUpload" :action="item.action" :max-count='10' @on-success="(event)=>{onSuccess(item,event)}"></u-upload>
						</view>
					</view>
				</view>
			</view>
			
			<view class="buttonBox">
				<view class="save" @click="save">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				contentJosn: null,
				title:'',
				eqName:'',
				nowTime:'',
				dataType:'',
				remark:'',
				fileList:[],
				show:false,
				htmlRes:[]
			}
		},
		onLoad(e) {
			uni.showLoading({
				title: '加载中'
			});
			this.eqName = e.eqName
			this.dataType = e.id
			this.$u.api.getOrderContent(e.id).then(res => {
				this.contentJosn = JSON.parse(res.data.formContent)
				this.getHtmlInfo()
				console.log(this.contentJosn);
				this.title = res.data.formName
				this.show = true
				uni.setNavigationBarTitle({
					title: res.data.formName
				})
				uni.hideLoading()
			})
			const data = new Date()
			this.nowTime = `${data.getFullYear()}-${data.getMonth()+1}-${data.getDate()} ${data.getHours()}:${data.getMinutes()}`
		},
		mounted() {
		},
		methods: {
			getItem(e,dc=''){
				const resItem = {}
					resItem.vModel = e.__vModel__
					resItem.tag = e.__config__.tag
					resItem.tagIcon = e.__config__.tagIcon
					resItem.defaultValue = e.__config__.defaultValue
					resItem.label = e.__config__.label
					resItem.required = e.__config__.required
					resItem.showLabel = e.__config__.showLabel
					resItem.showLabel = e.__config__.showLabel
					resItem.slot = e.__slot__
					resItem.span = e.__config__.span
					resItem.titleText = e.titleText
					resItem.action = e.action
					resItem.placeholder = e.placeholder
					if(dc){
						resItem.displayCondition = dc
					}
					if(e.displayCondition){
						resItem.displayCondition = dc+','+e.displayCondition
					}
				return resItem
			},
			getHtmlInfo(){
				this.contentJosn.fields.forEach(item=>{
					let resItemArr = []
					if(item.__config__.children){
						let displayCondition = ''
						if(item.displayCondition){
							displayCondition = item.displayCondition
						}
						resItemArr = this.forEachChildren(item.__config__.children,displayCondition)
					}else{
						const resItem = this.getItem(item)
						resItemArr.push(resItem)
					}
					this.htmlRes.push(resItemArr)
				})
			},
			forEachChildren(children,dc=''){
				const res =[]
				children.forEach(item=>{
					let resItem = {}
					if(item.__config__.children){
						let displayCondition = ''
						if(item.displayCondition){
							if(dc){
								displayCondition = dc+','+item.displayCondition
							}else{
								displayCondition = item.displayCondition
							}
						}
						const arr = this.forEachChildren(item.__config__.children,displayCondition)
						res.push(...arr)
					}else{
						resItem = this.getItem(item,dc)
						res.push(resItem)
					}
				})
				return res
			},
			isShowItem(item){
				let flag = true
				if(item.displayCondition){
					const dcArr = item.displayCondition.split(',')
					dcArr.forEach(e=>{
						const arr = e.split('/==/')
						this.htmlRes.forEach(group=>{
							group.forEach(res=>{
								if(res.vModel==arr[0]){
									if(res.defaultValue!=arr[1]){
										flag = false
									}
								}
							})
							
						})
						
					})
				}
				return flag
			},
			getStringifyCotent(){
				this.recursionSetValue(this.contentJosn.fields)
				return JSON.stringify(this.contentJosn)
			},
			recursionSetValue(children){
				children.forEach(item=>{
					if(item.__config__.children){
						this.recursionSetValue(item.__config__.children)
					}else if(item.__vModel__){
						this.htmlRes.forEach(group=>{
							group.forEach(res=>{
								if(res.vModel==item.__vModel__){
									item.__config__.defaultValue = res.defaultValue
								}
							})
						})
					}
				})
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e,val) {
				console.log(e);
			},
			save(){
				const date = new Date()
				const recordTime = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
				const content = this.getStringifyCotent()
				const userInfo = uni.getStorageSync('userInfo')
				const par = {
					type:this.dataType,
					content,
					recordTime,
					operationEmployeeId:userInfo.id,
					industrialEquipmentName:this.eqName,
					remark:this.remark,
					fileInfo:this.fileList.join(',')
				}
				const param =  {
					openid:uni.getStorageSync('openid'),
					param:JSON.stringify(par)
				}
				this.$u.api.checkpointRecord(param,{'content-type':'application/json'}).then(res => {
					console.log(res);
					uni.showToast({
						title: res.msg,
						icon: 'success',
						duration: 2000
					});
					uni.navigateBack()
				})
			},
			
			onSuccess(e,res) {
				if(e.__config__.defaultValue){
					e.__config__.defaultValue += `,${res.url}`
				}else{
					e.__config__.defaultValue = res.url
				}
				this.fileList.push(res.url) 
			}
		}
	}
</script>

<style lang="scss" scoped>
.fillOrderWrapper{
	width: 100%;
	background-color: #F5F9FC;
	box-sizing: border-box;
	padding: 20rpx;
	.title{
		width: 100%;
		height: 50rpx;
		position: relative;
		padding-left: 10rpx;
		line-height: 50rpx;
		font-weight: bold;
		&::before{
			content: '';
			width: 10rpx;
			height: 50rpx;
			position: absolute;
			top: 0%;
			left: 0%;
			transform: translateX(-100%);
			background-color: #05c;
		}
	}
	.infoBox{
		width: 100%;
		margin-bottom: 20rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding-top: 20rpx;
		padding-bottom: 40rpx;
		
		.remark{
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;
			text{
				flex-shrink: 0;
			}
			.el-input{
				width: 100%;
				flex-shrink: 1;
			}
		}
	}
	.itemWRapper{
		background-color: #f4f4f4;
		border-radius: 10rpx;
		width: 100%;
		box-sizing: border-box;
		padding: 15rpx;
	}
	
	.buttonBox{
		margin-top: 40rpx;
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		.save{
			padding: 15rpx;
			font-size: 26rpx;
			color: #fff;
			background-color: #00a0e9;
			border-radius: 16rpx;
			width: 200rpx;
			text-align: center;
		}
	}
	.comItemBox{
		width: 100%;
		box-sizing: border-box;
		padding: 10rpx;
		border-radius: 10rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-items: flex-start;
		margin-bottom: 20rpx;
		border-bottom: 2rpx solid #ddd;
		// background-color: #fff;
		.comItem{
			margin-bottom: 10rpx;
			display: flex;
			align-items: center;
			word-break: break-all;
			.title_label{
				margin-right: 10rpx;
				flex-shrink: 0;
			}
			.el-input{
				width: 100%;
				flex-shrink: 1;
			}
			.file{
				border: 2rpx solid #ddd;
			}
		}
	}
}
</style>
